package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.common.annotion.CmputerTime;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.redis.RedisUtils;
import com.stylefeng.guns.common.transform.ComputerManageTransform;
import com.stylefeng.guns.modular.system.dao.ComputerManageDao;
import com.stylefeng.guns.modular.system.dao.ComputerTypeDao;
import com.stylefeng.guns.common.persistence.model.ComputerManage;
import com.stylefeng.guns.common.persistence.model.ComputerType;
import com.stylefeng.guns.modular.system.service.IComputerManageService;
import com.stylefeng.guns.core.util.ToolUtil;
import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * computerManage控制器
 *
 * @author fengshuonan
 * @Date 2019-07-16 11:51:53
 */

@Controller
@RequestMapping("/computerManage")
public class ComputerManageController extends BaseController {

    private String PREFIX = "/computerManage/computerManage/";

    @Resource
    ComputerManageDao computerManageDao;

    @Resource
    IComputerManageService computerManageService;

    @Resource
    ComputerTypeDao computerTypeDao;

    @Autowired
    private  RedisUtils redisUtils;


    /**
     * 跳转到computerManage首页
     */
    @RequestMapping("")

    public String index() {
        return PREFIX + "computerManage.html";
    }

    /**
     * 跳转到添加computerManage
     */
    @RequestMapping("/computerManage_add")
    public String computerManageAdd(Model model) {
        EntityWrapper<ComputerType> wrapper =new EntityWrapper<>();
        List<ComputerType> list =computerTypeDao.selectList(wrapper);
        model.addAttribute("list",list);
        return PREFIX + "computerManage_add.html";
    }

    /**
     * 跳转到修改computerManage
     */
    @CmputerTime
    @RequestMapping("/computerManage_update/{id}")
    public String computerManageUpdate(@PathVariable Integer id, Model model) {
        ComputerManage computerManage=computerManageDao.selectById(id);
        ComputerType computerType =computerTypeDao.selectById(computerManage.getPid());
        EntityWrapper<ComputerType> wrapper =new EntityWrapper<>();
        List<ComputerType> list =computerTypeDao.selectList(wrapper);
        model.addAttribute("list",list);
        model.addAttribute(computerManage);
        model.addAttribute(computerType);
        return PREFIX + "computerManage_edit.html";
    }

    /**
     * 获取computerManage列表
     */
    @CmputerTime
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取数据")
    public Object list(@RequestParam(required = false)String condition) {
        List<ComputerManage> list = this.computerManageService.search(condition);
        for(ComputerManage computerManage:list){
            computerManage.getId();
        }
//        redisUtils.set("computerManage",list);
//        List<ComputerManage> manage =(List<ComputerManage>)redisUtils.get("computerManage");
//        System.err.println(manage);
//        Jedis jedis = new Jedis("localhost");
//        TimerTask timerTask=new TimerTask() {
//            @Override
//            public void run() {
//                jedis.flushDB();
//            }
//        };
//        Timer timer=new Timer();
//        timer.schedule(timerTask,0,1000*60*5);

        return ComputerManageTransform.transform(list);
    }

    /**
     * 新增computerManage
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("添加数据")
    public Object add(@ModelAttribute ComputerManage computerManage) {
        try {
            computerManage.setCreatetime(new Date());
            computerManage.setModifiedtime(new Date());
            computerManageDao.insert(computerManage);
            return super.SUCCESS_TIP;
        }catch (Exception e){
            return super.ERROR;
        }
    }

    /**
     * 删除computerManage
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("删除数据")
    public Object delete(@RequestParam("id") Integer id) {
        try {
            computerManageDao.deleteById(id);
            return SUCCESS_TIP;
        }catch (Exception e){
            return super.ERROR;
        }

    }


    /**
     * 修改computerManage
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("修改数据")
    public Object update(@ModelAttribute ComputerManage computerManage) {
        try {
            if (ToolUtil.isEmpty(computerManage) || computerManage.getId() == null) {
                throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
            }

            computerManage.setModifiedtime(new Date());
            computerManageDao.updateById(computerManage);
            return super.SUCCESS_TIP;
        }catch (Exception e){
            return super.ERROR;
        }

    }

    /**
     * computerManage详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}